﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using Lists;
using GeneralLibs;

namespace ReportGenerators.MuaHang
{
    public partial class FrmThongKeNhapXuatTon : BasicForms.FrmSupportPrintableComponents
    {
        public FrmThongKeNhapXuatTon()
        {
            InitializeComponent();
        }

        private void FrmThongKeNhapXuatTon_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            try
            {
                cmbKho.DataSource = DanhMucKho.Instance.DataView;
                cmbKho.DisplayColumn = "Ten";
                cmbKho.ValueColumn = "ID";
                cmbKho.AddVisibleColumn("Ma", "Mã");
                cmbKho.AddVisibleColumn("Ten", "Tên");
                cmbKho.EditValue = Guid.Empty;

                cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Instance.DataView;
                cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
                cmbNhomVatTuHangHoa.ValueColumn = "ID";
                cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
                cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

                cmbKyBaoCao.SelectedIndex = 19;
                txtGioiHanTren.EditValue = 1000000000;
                txtGioiHanDuoi.EditValue = 0;

                dsThongKeNhapXuatTon.VatTuHangHoa.Merge(DanhMucVatTuHangHoa.GetDanhMucHangHoaDangSuDung().DataView.ToTable());  //Chỉ lấy những thuốc TrangThai=1.
            }
            catch (Exception ex)
            {
            }

            try
            {
                foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridViewDsVatTuHangHoa.Columns)
                {
                    column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                }
                foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
                {
                    column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                }
                //foreach (DataRow row in dsThongKeNhapXuatTon.VatTuHangHoa.Rows)
                //{
                //    row["Choose"] = false;
                //}
                chkChooseAllVatTuHangHoa.Checked = false;
                FormatTextEdit(txtGioiHanTren, TextFormatType.Money);
                FormatTextEdit(txtGioiHanDuoi, TextFormatType.Money);
                FormatNumberInGridView(gridViewDsVatTuHangHoa);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void chkChooseAllVatTuHangHoa_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataRow row in dsThongKeNhapXuatTon.VatTuHangHoa.Rows)
            {
                row["Choose"] = chkChooseAllVatTuHangHoa.Checked;
            }
        }

        private void cmbNhomVatTuHangHoa_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete) cmbNhomVatTuHangHoa.EditValue = Guid.Empty;
        }
        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            //if (!(cmbNhomVatTuHangHoa.EditValue is Guid) || (Guid)cmbNhomVatTuHangHoa.EditValue == Guid.Empty) return;
            try
            {
                if (cmbNhomVatTuHangHoa.EditValue is Guid && (Guid)cmbNhomVatTuHangHoa.EditValue != Guid.Empty)
                {
                    //Dieu kien loc la ID = cmbNhomVatTuHangHoa.EditValue + ID cac nhom con.
                    string filterNhomCon = "CONVERT('{" + cmbNhomVatTuHangHoa.EditValue.ToString() + "}','System.Guid'), ";
                    DanhMucNhomVatTuHangHoa cacNhomCon = DanhMucNhomVatTuHangHoa.Instance.GetNhomVatTuHangHoaCon((Guid)cmbNhomVatTuHangHoa.EditValue);
                    foreach (NhomVatTuHangHoa nhomCon in cacNhomCon)
                    {
                        filterNhomCon += "CONVERT('{" + nhomCon.ID + "}','System.Guid'), ";
                        //filterNhomCon += string.Format(", '{0}'", nhomCon.ID);
                    }
                    if (filterNhomCon.EndsWith(", ")) filterNhomCon = filterNhomCon.Remove(filterNhomCon.Length - 2);

                    dsThongKeNhapXuatTon.VatTuHangHoa.Clear();
                    string filter = string.Format("NhomVatTuHangHoaID IN ({0}) AND TrangThai = 1 ", filterNhomCon);
                    dsThongKeNhapXuatTon.VatTuHangHoa.Merge(DanhMucVatTuHangHoa.Filter(filter).DataView.ToTable());
                }
                else
                {
                    dsThongKeNhapXuatTon.VatTuHangHoa.Clear();
                    dsThongKeNhapXuatTon.VatTuHangHoa.Merge(DanhMucVatTuHangHoa.GetDanhMucHangHoaDangSuDung().DataView.ToTable());
                }
                chkChooseAllVatTuHangHoa.Checked = false;
                chkChooseAllVatTuHangHoa.Checked = true;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void cmbKho_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete) cmbKho.EditValue = Guid.Empty;
        }

        private DataTable GetHangDaCoGiaoDich(SqlCommand cmd, Guid selectedKhoID, DateTime ngayKetThuc)
        {
            try
            {
                ThoiDiemKhoaSo thoiDiemKhoaSo = DanhMucThoiDiemKhoaSo.Instance.GetThoiDiemChotSoDuGanNhat(ngayKetThuc);
                if (selectedKhoID != null && selectedKhoID != Guid.Empty)
                {
                    cmd.CommandText = string.Format("SELECT DISTINCT A.VatTuHangHoaID, KhoID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM NhatKyKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID \n" +
                                                    "WHERE  KhoID = @KhoID AND @ThoiDiemDau <= Ngay AND Ngay <= @ThoiDiemCuoi \n" +
                                                    "   AND A.VatTuHangHoaID is not null  \n" +
                                                    "UNION \n" +
                                                    "SELECT DISTINCT A.VatTuHangHoaID, KhoID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM TonKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID  \n" +
                                                    "WHERE KhoID = @KhoID AND SoLuong <> 0 \n" +
                                                    "   AND A.VatTuHangHoaID is NOT NULL");
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("KhoID", selectedKhoID);
                    cmd.Parameters.AddWithValue("ThoiDiemDau", thoiDiemKhoaSo.ThoiDiem);
                    cmd.Parameters.AddWithValue("ThoiDiemCuoi", ngayKetThuc);
                }
                else
                {
                    cmd.CommandText = string.Format("SELECT DISTINCT A.VatTuHangHoaID, B.NhomVatTuHangHoaID \n" +
                                              "FROM NhatKyKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID \n" +
                                              "WHERE  @ThoiDiemDau <= Ngay AND Ngay <= @ThoiDiemCuoi  \n" +
                                              "	AND A.VatTuHangHoaID is NOT NULL \n" +
                                              "UNION \n" +
                                              "SELECT DISTINCT VatTuHangHoaID, B.NhomVatTuHangHoaID  \n" +
                                              "FROM TonKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID  \n" +
                                              "WHERE SoLuong <> 0 \n" +
                                              "	AND A.VatTuHangHoaID is NOT NULL ");

                    cmd.Parameters.Clear();
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("ThoiDiemDau", thoiDiemKhoaSo.ThoiDiem);
                    cmd.Parameters.AddWithValue("ThoiDiemCuoi", ngayKetThuc);
                }
                DataTable dtDaCoGiaoDich = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dtDaCoGiaoDich);
                return dtDaCoGiaoDich;
            }
            catch (Exception ex)
            {
                string message = string.Format("Lỗi [{0}] ở \n {1}", ex.Message, ex.StackTrace.Substring(0, ex.StackTrace.IndexOf("\n")));
                throw new CustomException(message);
                return null;
            }
        }

        private DataTable GetToanBoHangDangSuDung(SqlCommand cmd, Guid selectedKhoID, DateTime ngayKetThuc)
        {
            try
            {
                ThoiDiemKhoaSo thoiDiemKhoaSo = DanhMucThoiDiemKhoaSo.Instance.GetThoiDiemChotSoDuGanNhat(ngayKetThuc);
                if (selectedKhoID != null && selectedKhoID != Guid.Empty)
                {
                    cmd.CommandText = string.Format("SELECT DISTINCT A.VatTuHangHoaID, KhoID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM NhatKyKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID  \n" +
                                                    "WHERE  KhoID = @KhoID AND @ThoiDiemDau <= Ngay AND Ngay <= @ThoiDiemCuoi \n" +
                                                    "   AND A.VatTuHangHoaID is not null  \n" +
                                                    "UNION \n" +
                                                    "SELECT DISTINCT A.VatTuHangHoaID, KhoID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM TonKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID  \n" +
                                                    "WHERE KhoID = @KhoID AND SoLuong <> 0 \n" +
                                                    "   AND A.VatTuHangHoaID is NOT NULL");
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("KhoID", selectedKhoID);
                    cmd.Parameters.AddWithValue("ThoiDiemDau", thoiDiemKhoaSo.ThoiDiem);
                    cmd.Parameters.AddWithValue("ThoiDiemCuoi", ngayKetThuc);
                }
                else
                {

                    cmd.CommandText = string.Format("SELECT DISTINCT VatTuHangHoaID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM NhatKyKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID \n" +
                                                    "WHERE  @ThoiDiemDau <= Ngay AND Ngay <= @ThoiDiemCuoi  \n" +
                                                    "	AND A.VatTuHangHoaID is NOT NULL \n" +
                                                    "UNION \n" +
                                                    "SELECT DISTINCT VatTuHangHoaID, B.NhomVatTuHangHoaID \n" +
                                                    "FROM TonKho A INNER JOIN VatTuHangHoa B ON B.ID = A.VatTuHangHoaID  \n" +
                                                    "WHERE SoLuong <> 0 \n" +
                                                    "	AND A.VatTuHangHoaID is NOT NULL ");

                    cmd.Parameters.Clear();
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("ThoiDiemDau", thoiDiemKhoaSo.ThoiDiem);
                    cmd.Parameters.AddWithValue("ThoiDiemCuoi", ngayKetThuc);
                }
                DataTable dtDaCoGiaoDich = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dtDaCoGiaoDich);
                return dtDaCoGiaoDich;
            }
            catch (Exception ex)
            {
                string message = string.Format("Lỗi [{0}] ở \n {1}", ex.Message, ex.StackTrace.Substring(0, ex.StackTrace.IndexOf("\n")));
                throw new CustomException(message);
                return null;
            }
        }
            
        private void btnLoad_Click(object sender, EventArgs e)
        {
            DateTime ngayBatDau = new DateTime(); if (LocalSettings.HeThong.CheDoBiKiemTra) ngayBatDau = DateTime.Now.AddDays(-2); else ngayBatDau = dtNgayBatDau.DateTime; //DateTime ngayBatDau = dtNgayBatDau.DateTime;
            DateTime ngayKetThuc = dtNgayKetThuc.DateTime;
            if(!(FrmThamSoBaoCao.CheckKyBaoCaoVaThoiDiemKhoaSo(dtNgayBatDau, dtNgayKetThuc))) return;
            ThoiDiemKhoaSo thoiDiemKhoaSoGanNhat = DanhMucThoiDiemKhoaSo.Instance.GetThoiDiemKhoaSoGanNhat(ngayKetThuc);

            int gioiHanSoLuongTu = (int)txtGioiHanDuoi.EditValue;
            int gioiHanSoLuongDen = (int)txtGioiHanTren.EditValue;

            colTonDauKy.Caption = "Tồn " + dtNgayBatDau.Text;
            colSoLuongTon.Caption = "Tồn " + dtNgayKetThuc.Text; 
            Guid selectedKhoID = Guid.Empty;
            if(cmbKho.EditValue is Guid && (Guid)cmbKho.EditValue != Guid.Empty)
                selectedKhoID = (Guid)cmbKho.EditValue;
            
            BasicForms.FrmProgressBarDev.StartShowProgress();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = GeneralLibs.Config.TimeOut;

                string vatTuHangHoaCondition ="";
                if (chkChooseAllVatTuHangHoa.Checked)
                {
                    if (cmbNhomVatTuHangHoa.Text == "")
                        vatTuHangHoaCondition = "1=1";
                    else
                        vatTuHangHoaCondition = string.Format("NhomVatTuHangHoaID = '{0}'", cmbNhomVatTuHangHoa.EditValue);
                }
                else
                {
                    if (chkShowHangDaCoGiaoDich.Checked) //Nếu lọc thuốc đã giao dịch thì để VatTuHangHoaID
                        vatTuHangHoaCondition = "VatTuHangHoaID IN (";
                    else //Nếu load tất thuốc từ danh mục thì cột VatTuHangHoaID chuyển thành ID
                        vatTuHangHoaCondition = "ID IN (";
                    foreach (DataRow row in dsThongKeNhapXuatTon.VatTuHangHoa.Rows)
                    {
                        if (row["Choose"] is bool && (bool)row["Choose"])
                        {
                            vatTuHangHoaCondition += string.Format("CONVERT('{0}', System.Guid), ", (Guid)row["ID"]);
                        }
                    }
                    if (vatTuHangHoaCondition.EndsWith(", "))
                        vatTuHangHoaCondition = vatTuHangHoaCondition.Substring(0, vatTuHangHoaCondition.Length - 2) + ")";
                    else vatTuHangHoaCondition = "1 = 0";
                }

                if (selectedKhoID != null && selectedKhoID != Guid.Empty)
                {
                    DataTable dtDaCoGiaoDich = new DataTable();
                    //Nếu option chỉ chon những thuốc đã giao dịch thì chỉ get những thuốc đã có giao dịch
                    if (chkShowHangDaCoGiaoDich.Checked)
                        dtDaCoGiaoDich = GetHangDaCoGiaoDich(cmd, selectedKhoID, ngayKetThuc);
                    else //Nếu không thì lấy toàn bộ danh mục
                        dtDaCoGiaoDich = DanhMucVatTuHangHoa.Instance.DataTable;

                    dtDaCoGiaoDich.DefaultView.RowFilter = vatTuHangHoaCondition;
                    dtDaCoGiaoDich = dtDaCoGiaoDich.DefaultView.ToTable();

                    dsThongKeNhapXuatTon.Detail.Clear();

                    int count = 0;
                    foreach (DataRow row in dtDaCoGiaoDich.Rows)
                    {
                        BasicForms.FrmProgressBarDev.Status = string.Format("Đang xử lý {0}/{1}", count, dtDaCoGiaoDich.Rows.Count);
                        count++;

                        Guid vatTuHangHoaID;
                        if (chkShowHangDaCoGiaoDich.Checked)
                            vatTuHangHoaID = (Guid)row["VatTuHangHoaID"];
                        else
                            vatTuHangHoaID = (Guid)row["ID"];

                        VatTuHangHoa vatTuHangHoa = DanhMucVatTuHangHoa.Instance.GetByID(vatTuHangHoaID);
                        Guid khoID = (Guid)row["KhoID"];

                        //tonkho tu dau ky gan nhat den ngay dau tien rong khoang thoi gian can xem
                        TonKho tonKho = TonKho.CalculateTonTuThoiDiemKhoaSoGanNhatDenThoiDiemNhapVaoTheoKho(cmd, ngayBatDau, khoID, vatTuHangHoaID);
                        //phat sinh nhap xuat trong khoang thoi gian can xem
                        TonKho phatSinhNhap = TonKho.CalculatePhatSinhNhap(cmd, ngayBatDau.AddSeconds(1), ngayKetThuc, khoID, vatTuHangHoaID);
                        TonKho phatSinhXuat = TonKho.CalculatePhatSinhXuat(cmd, ngayBatDau.AddSeconds(1), ngayKetThuc, khoID, vatTuHangHoaID);

                        decimal donGiaVon = (decimal)DanhMucVatTuHangHoa.Instance.GetGiaVonTrungBinhTheoGiaTriConLaiToanBoTheoKhoByID(vatTuHangHoa.ID, selectedKhoID, thoiDiemKhoaSoGanNhat.ID, ngayBatDau, ngayKetThuc);
                        decimal soLuongTon = tonKho.SoLuong + phatSinhNhap.SoLuong - phatSinhXuat.SoLuong;

                        
                        if (chkShowHangDaCoGiaoDich.Checked)
                        {
                            if ((gioiHanSoLuongTu <= soLuongTon && soLuongTon <= gioiHanSoLuongDen) && (tonKho.SoLuong != 0 || soLuongTon != 0 || phatSinhNhap.SoLuong != 0 || phatSinhXuat.SoLuong != 0))
                            {
                                DataRow addedRow = dsThongKeNhapXuatTon.Detail.NewRow();
                                addedRow["Ma"] = vatTuHangHoa.Ma;
                                addedRow["Ten"] = vatTuHangHoa.Ten;
                                addedRow["GiaBia"] = vatTuHangHoa.GiaBia;
                                addedRow["TonDau"] = tonKho.SoLuong;
                                addedRow["SoLuongNhap"] = phatSinhNhap.SoLuong;
                                addedRow["SoLuongXuat"] = phatSinhXuat.SoLuong;
                                addedRow["SoLuongTon"] = soLuongTon;
                                addedRow["DonGiaVon"] = donGiaVon;
                                addedRow["ThanhTienVon"] = soLuongTon * donGiaVon;
                                dsThongKeNhapXuatTon.Detail.Rows.Add(addedRow);
                            }
                        }
                        else
                        {
                            if ((gioiHanSoLuongTu <= soLuongTon && soLuongTon <= gioiHanSoLuongDen))
                            {
                                DataRow addedRow = dsThongKeNhapXuatTon.Detail.NewRow();
                                addedRow["Ma"] = vatTuHangHoa.Ma;
                                addedRow["Ten"] = vatTuHangHoa.Ten;
                                addedRow["GiaBia"] = vatTuHangHoa.GiaBia;
                                addedRow["TonDau"] = tonKho.SoLuong;
                                addedRow["SoLuongNhap"] = phatSinhNhap.SoLuong;
                                addedRow["SoLuongXuat"] = phatSinhXuat.SoLuong;
                                addedRow["SoLuongTon"] = soLuongTon;
                                addedRow["DonGiaVon"] = donGiaVon;
                                addedRow["ThanhTienVon"] = soLuongTon * donGiaVon;
                                dsThongKeNhapXuatTon.Detail.Rows.Add(addedRow);
                            }
                        }
                    }
                }
                else
                {
                    DataTable dtDaCoGiaoDich = new DataTable();
                    //Nếu option chỉ chon những thuốc đã giao dịch thì chỉ get những thuốc đã có giao dịch
                    if (chkShowHangDaCoGiaoDich.Checked)
                        dtDaCoGiaoDich = GetHangDaCoGiaoDich(cmd, selectedKhoID, ngayKetThuc);
                    else //Nếu không thì lấy toàn bộ danh mục
                        dtDaCoGiaoDich = DanhMucVatTuHangHoa.Instance.DataTable;

                    dtDaCoGiaoDich.DefaultView.RowFilter = vatTuHangHoaCondition;
                    dtDaCoGiaoDich = dtDaCoGiaoDich.DefaultView.ToTable();
                    dsThongKeNhapXuatTon.Detail.Clear();
                    int count = 0;
                    foreach (DataRow row in dtDaCoGiaoDich.Rows)
                    {
                        BasicForms.FrmProgressBarDev.Status = string.Format("Đang xử lý {0}/{1}", count, dtDaCoGiaoDich.Rows.Count);
                        count++;

                        Guid vatTuHangHoaID;
                        if (chkShowHangDaCoGiaoDich.Checked) 
                            vatTuHangHoaID = (Guid)row["VatTuHangHoaID"];
                        else
                            vatTuHangHoaID = (Guid)row["ID"];

                        VatTuHangHoa vatTuHangHoa = DanhMucVatTuHangHoa.Instance.GetByID(vatTuHangHoaID);
                        decimal donGiaVon = (decimal)DanhMucVatTuHangHoa.Instance.GetGiaVonTrungBinhTheoGiaTriConLaiToanBoCacKhoByID(vatTuHangHoa.ID, thoiDiemKhoaSoGanNhat.ID, ngayBatDau, ngayKetThuc);

                        //Tính tồn kho cũ. Chính xác nhưng chậm
                        TonKho tonToanBo = TonKho.CalculateTongTonTuThoiDiemKhoaSoGanNhatDenThoiDiemNhapVao(cmd, ngayBatDau, vatTuHangHoaID);
                        TonKho phatSinhNhapToanBo = TonKho.CalculatePhatSinhNhapToanBo(cmd, ngayBatDau, ngayKetThuc, vatTuHangHoaID);
                        TonKho phatSinhXuatToanBo = TonKho.CalculatePhatSinhXuatToanBo(cmd, ngayBatDau, ngayKetThuc, vatTuHangHoaID);
                        decimal soLuongTonToanBo = tonToanBo.SoLuong + phatSinhNhapToanBo.SoLuong - phatSinhXuatToanBo.SoLuong;

                        if (chkShowHangDaCoGiaoDich.Checked)
                        {
                            if ((gioiHanSoLuongTu <= soLuongTonToanBo && soLuongTonToanBo <= gioiHanSoLuongDen) && (tonToanBo.SoLuong != 0 || tonToanBo.SoLuong != 0 || phatSinhNhapToanBo.SoLuong != 0 || phatSinhXuatToanBo.SoLuong != 0))
                            {
                                DataRow addedRow = dsThongKeNhapXuatTon.Detail.NewRow();
                                addedRow["Ma"] = vatTuHangHoa.Ma;
                                addedRow["Ten"] = vatTuHangHoa.Ten;
                                addedRow["GiaBia"] = vatTuHangHoa.GiaBia;
                                addedRow["TonDau"] = tonToanBo.SoLuong;
                                addedRow["SoLuongNhap"] = phatSinhNhapToanBo.SoLuong;
                                addedRow["SoLuongXuat"] = phatSinhXuatToanBo.SoLuong;
                                addedRow["SoLuongTon"] = soLuongTonToanBo;
                                addedRow["DonGiaVon"] = donGiaVon;
                                addedRow["ThanhTienVon"] = soLuongTonToanBo * donGiaVon;
                                dsThongKeNhapXuatTon.Detail.Rows.Add(addedRow);
                            }
                        }
                        else
                        {
                            if ((gioiHanSoLuongTu <= soLuongTonToanBo && soLuongTonToanBo <= gioiHanSoLuongDen))
                            {
                                DataRow addedRow = dsThongKeNhapXuatTon.Detail.NewRow();
                                addedRow["Ma"] = vatTuHangHoa.Ma;
                                addedRow["Ten"] = vatTuHangHoa.Ten;
                                addedRow["GiaBia"] = vatTuHangHoa.GiaBia;
                                addedRow["TonDau"] = tonToanBo.SoLuong;
                                addedRow["SoLuongNhap"] = phatSinhNhapToanBo.SoLuong;
                                addedRow["SoLuongXuat"] = phatSinhXuatToanBo.SoLuong;
                                addedRow["SoLuongTon"] = soLuongTonToanBo;
                                addedRow["DonGiaVon"] = donGiaVon;
                                addedRow["ThanhTienVon"] = soLuongTonToanBo * donGiaVon;
                                dsThongKeNhapXuatTon.Detail.Rows.Add(addedRow);
                            }
                        }
                    }
                }
                FormatNumberInGridView(gridView);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                FormatNumberInGridView(gridViewDsVatTuHangHoa);
                con.Close();
                con = null;
            }
            BasicForms.FrmProgressBarDev.FinishShowProgress();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            PrintWithHeaderAndFooter(false, gridControl, true, true, "Thống kê chi tiết nhập xuất tồn", "", "", "", "");
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FrmThamSoBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dtNgayBatDau, dtNgayKetThuc);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void chkShowHangDaCoGiaoDich_CheckedChanged(object sender, EventArgs e)
        {
            //Nếu Chọn hiện tất các thuốc thì không xem theo kho được
            //Vì thuốc chưa có mua bán nào thì không có ở kho nào cả
            if (chkShowHangDaCoGiaoDich.Checked)
            {
                cmbKho.Visible = true;
                lblTheoKho.Visible = true;
            }
            else
            {
                cmbKho.EditValue = Guid.Empty;
                cmbKho.Visible = false;
                lblTheoKho.Visible = false;
            }
        }
    }
}
